package  engine.display.newAttack.action
{
	import com.greensock.TweenMax;
	import com.greensock.easing.Linear;
	
	import engine.display.newAttack.AttackPlayer;
	import engine.display.newAttack.action.effect.PointPlayLite;
	import engine.display.newAttack.vo.AttackActionVo;
	import engine.display.newAttack.vo.DamageVo;
	import engine.display.newAttack.vo.FlowVo;
	import engine.flag.PlayerActionFlag;
	import engine.logic.McPool;
	
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.geom.Point;
	import flash.utils.setTimeout;
	
	public class PhyCloseAttack extends AttackAction
	{
		protected var _fPlayerSave		:AttackPlayer;
		/**施法者*/
		protected var _fPlayer			:AttackPlayer;
		
		/**受伤者*/
		protected var _tPlayer			:AttackPlayer;
		
		protected var _damageVos		:Vector.<DamageVo>;
		
		protected var _flowVo			:FlowVo;
		
		protected var _attackActionVo	:AttackActionVo;
		
		protected var _endPoint		:Point;
		protected var _startPoint		:Point;
		
		protected var _runTime			:Number;
		
		/**待击中次数*/
		protected var _hitCount		:int		=	1;
		
		/**已击中次数*/
		protected var _cuHitCount		:int		=	0;
		
		/**攻击结束*/
		protected var _isEnd			:Boolean	=	false;
		
//		/**受伤特效*/
//		private var _beAttactEffect	:String;
		
		/**播放过程中触发*/
		protected var _onPlay			:Function;
		
		/**播放完成后播放*/
		protected var _onPlayOver		:Function;
		
		protected var _soundHit		:String;
		
		public function PhyCloseAttack(fPlayer:AttackPlayer, 
									   tPlayers:Array, 
									   flowVo : FlowVo, 
									   attackActionVo:AttackActionVo,
									   onPlay:Function, 
									   onPlayOver:Function=null, 
									   soundHit:String="")		//击打声音
		{
			_fPlayer			=	fPlayer;
			_tPlayer			=	tPlayers.shift();
			_fPlayerSave		=	_fPlayer;
			
			_flowVo				=	flowVo;
			_damageVos			=	flowVo.damageVos;
			
			_attackActionVo		=	attackActionVo;
			
			_onPlay				=	onPlay;
			_onPlayOver			=	onPlayOver;
			
			_soundHit			=	soundHit;
			
			init()
		}
		
		private function init():void
		{
			//计算跑动时间
			var speed : Number = _attackActionVo.runOverSpeed;//player.GetProperties("YLZG_RunSpeed");
			
			_runTime =	Point.distance( new Point(_fPlayer.x,_fPlayer.y), new Point(_tPlayer.x,_tPlayer.y) ) / speed;
			
			_startPoint		=	new Point( _fPlayer.initX, _fPlayer.initY );
			_endPoint		=	new Point( _tPlayer.initX, _tPlayer.initY );
			
			var offset:Point= new Point( 90, 0 );
			
			if( _fPlayer.x < _tPlayer.x )
			{
				_endPoint.x	=	_endPoint.x - offset.x;
			}
			else
			{
				_endPoint.x	=	_endPoint.x + offset.x;
			}
			_endPoint.y		=	_tPlayer.y + offset.y;
		}
		
		public function play():void
		{
			_isEnd	=	false;
			
			if( _attackActionVo.needSceneBlack )
			{
				//黑场景
				playBlackEffect();
				
				setTimeout( playStartEffect,BLACKSCENE_EFFECT_DELAY,BLACKSCENE_EFFECT_DELAY );
			}
			else
			{
				playStartEffect();
			}
		}
		
		/**
		 * 播放开始特效
		 * */
		protected function playStartEffect(val:int = 0):void
		{
//			//播放释放技能前的特效，如果有
//			if( _attackActionVo.startEffectData != null)
//			{
//				new PointPlayLite(_fPlayer.effectPlace,
//					_fPlayer.originPoint,
//					McPool.borrowRes( _attackActionVo.startEffectData["skin"] ),
//					_attackActionVo.startEffectData);
//				
//				if( 0 != _attackActionVo.startEffecDelay  )
//				{
//					setTimeout( runToAttack, _attackActionVo.startEffecDelay - val );
//				}
//				else
//				{
//					runToAttack();
//				}
//			}
//			else
//			{
				runToAttack();
//			}
		}
		
		protected function onPlayAttack(e:Event):void
		{
//			if( _fPlayer.getSkin().currentLabel == PlayerActionFlag.Normal  )
//			{
//				if( _attackActionVo.effectSkin != null )
//				{
//					new PointPlayLite(_tPlayer.effectPlace,
//						_tPlayer.sPosPoint,
//						McPool.borrowRes( _attackActionVo.effectSkin ),
//						_attackActionVo.beAttactEffectData);
//				}
//			}
		}
		
		protected function attackToRun():void
		{
			if( _attackActionVo.needSceneBlack )
			{
				//亮场景
				removeBlackEffect();					
			}
			
			//跑回<-
			if(_fPlayerSave.team == 0)
			{
				_fPlayerSave.playToTarget(2);
			}
			else
			{
				_fPlayerSave.playToTarget(1);
			}
			
			_fPlayerSave.getSkin().filters = [ new BlurFilter( 200, 0 )];
			
			TweenMax.to(_fPlayerSave.getSkin(), _runTime * .5, 
				{	blurFilter:{blurX:0}
					})
			
			TweenMax.to(_fPlayerSave, _runTime * .5, 
				{	x			:_startPoint.x, 
					y			:_startPoint.y, 
					ease		:Linear.easeNone,
					onUpdate	:onRunning,
					onComplete	:onRunOver})
		}
		
		/**
		 * 起跑攻击
		 * @param val 加速时间
		 * 
		 */
		protected function runToAttack():void
		{
			if( _fPlayer.team == 0 )
			{
				_fPlayer.playToTarget(1);
			}
			else
			{
				_fPlayer.playToTarget(2);
			}
			
			_fPlayer.getSkin().filters = [ new BlurFilter( 200, 0 ) ];
			
			TweenMax.to(_fPlayer.getSkin(), _runTime * .5, 
				{	blurFilter:{blurX:0}
				})
			
			TweenMax.to(_fPlayer, _runTime,{x:_endPoint.x, 
				y:_endPoint.y, ease:Linear.easeNone,onUpdate:onRunning,onComplete:onRunTo})
		}
		
		protected function onRunning():void
		{
			_onPlay.call(this);
		}
		
		/**跑到*/
		private function onRunTo():void
		{
			_fPlayer.playToTarget( 0 );
			setTimeout( function():void
			{
				_fPlayer.getSkin().filters = null;
				_fPlayer.addEventListener(Event.ENTER_FRAME,onPlayAttack);
				_fPlayer.playToTarget( _attackActionVo.skillAttackType );
				
				onRunning();
			}, 100 );
			
			
			//如果有技能特效，播技能特效
		}
		
		protected function onRunOver():void
		{
			_fPlayerSave.playToTarget(0);
			
			if( null != _onPlayOver )
			{
				_onPlayOver();
			}
		}
	}
}